﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.ComponentModel.DataAnnotations;

using MvcContrib.FluentHtml;

using Revis.VClinicNet.App.Internal.Dto;
using Revis.VClinicNet.Core.Domain.Credentials;
using Revis.VClinicNet.Util.Translation;

namespace Revis.VClinicNet.UI.Internal.Mvc.Models
{
    public class VisitCreateModel : VisitCreateDto
    {
        [Required(ErrorMessage = "Podanie godziny odbycia wizyty jest niezbędne")]
        [Range(0, 23, ErrorMessage = "Godzina odbycia wizyty powinna być z zakresu 0-23")]
        public int Hour { get; set; }

        [Required(ErrorMessage = "Podanie minuty odbycia wizyty jest niezbędne")]
        [Range(0, 59, ErrorMessage = "Minuta odbycia wizyty powinna być z zakresu 0-59")]
        public int Minute { get; set; }

        public string PatientName { get; set; }

        [Required(ErrorMessage = "Podanie daty odbycia wizyty jest niezbędne")]
        public string DateAsString { get; set; }

        public new DateTime Date
        {
            get
            {
                DateTime date;
                if (DateTime.TryParse(DateAsString, out date)) {
                    date = date.AddHours(Hour).AddMinutes(Minute);
                } else {
                    date = DateTime.Now;
                }
                return date;
            }
        }

        public VisitCreateModel() { }

        public VisitCreateModel(VisitCreateModel model)
        {
            Hour = model.Hour;
            Minute = model.Minute;
            PatientName = model.PatientName;
            PatientId = model.PatientId;
            DoctorId = model.DoctorId;
            DateAsString = model.DateAsString;
        }
    }

    public class VisitCreateFormModel : VisitCreateModel
    {
        public IEnumerable<SelectListItem> Doctors { get; protected set; }

        public VisitCreateFormModel(IList<DoctorInfoDto> doctors, DateTime baseDate) :
            this(new VisitCreateModel(), doctors, baseDate)
        {
        }

        public VisitCreateFormModel(VisitCreateModel model, IList<DoctorInfoDto> doctors) :
            this(model, doctors, model.Date)
        {
        }

        public VisitCreateFormModel(VisitCreateModel model, IList<DoctorInfoDto> doctors, DateTime baseDate) :
            base(model)
        {
            doctors = doctors ?? new List<DoctorInfoDto>();
            Doctors = doctors.Select(d => {
                return new SelectListItem() {
                    Text = d.Name,
                    Value = d.Id.ToString(),
                    Selected = model.DoctorId == d.Id
                };
            });
            DateAsString = baseDate.ToShortDateString();
        }
    }
}